package org.jeecg.modules.yc.NB.Controller;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.math3.analysis.function.Add;
import org.jeecg.modules.yc.NB.Model.AddDeleteDevice;
import org.jeecg.modules.yc.NB.Model.DataReport;
import org.jeecg.modules.yc.NB.Model.UpAndDown;
import org.jeecg.modules.yc.NB.Services.NBNotificationService;
import org.jeecg.modules.yc.util.TransformationTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 */
@RestController
@RequestMapping("/NB")
public class NBNotificationController {

    @Autowired
    private NBNotificationService nbNotificationService;

    /**
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @ApiOperation(value = "**************************设备指令响应通知************************")
    @RequestMapping(value = "/instructions", method = RequestMethod.POST)
    public void Instructions(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        System.out.println("设备指令响应通知");
       /* nbService.equipmentCommandResponse(request, response);*/
    }

    @ApiOperation(value = "设备上下线通知")
    @RequestMapping(value = "/upAndDown", method = RequestMethod.POST)
    public void upAndDown(@RequestBody UpAndDown upAndDown, HttpServletResponse response)
            throws Exception {
      /*  System.out.println("设备上下线:  "+(AddDeleteDevice)upAndDown);
        System.out.println("设备上下线:  "+upAndDown.getTenantId());*/
        System.out.println("设备上下线:  "+upAndDown);
        nbNotificationService.upAndDown(upAndDown);
    }

    @ApiOperation(value = "**************************设备数据变化通知************************")
    @PostMapping(value = "/dataReport")
    public void dataReport(@RequestBody DataReport dataReport, HttpServletRequest request)
            throws Exception {
        System.out.println(dataReport);
        nbNotificationService.dataReport(dataReport);
       /* ObjectMapper mapper = new ObjectMapper();
        JsonNode rootNode = mapper.readTree(msg);
        JsonNode payload = rootNode.get("payload");
        String appData = payload.get("APPdata").asText();
        byte[] x = Base64.getDecoder().decode(appData);
        String agreementMessage = Hex.encodeHexString(x);//协议报文
        System.out.println(agreementMessage);*/

    }

    @ApiOperation(value = "**************************设备指令响应通知************************")
    @PostMapping(value = "/addDeleteDevice")
    public void createOrDelete(@RequestBody AddDeleteDevice addDeleteDevice, HttpServletRequest request)
            throws Exception {
        System.out.println(JSON.toJSONString("增加删除————————"+addDeleteDevice));
        //nbNotificationService.createOrDelete(addDeleteDevice);
    }

    @ApiOperation(value = "**************************设备指令响应通知************************")
    @PostMapping(value = "/report")
    public void deviceUpdate(@RequestBody String msg, HttpServletRequest request)
            throws Exception {
        System.out.println("设备上报____"+msg);
       // msg.getDeviceList().get(1).getDeviceName();
    }

    @ApiOperation(value = "**************************设备指令响应通知************************")
    @PostMapping(value = "/responseInstruction")
    public void responseInstruction(@RequestBody String msg, HttpServletRequest request)
            throws Exception {
        System.out.println("指令响应____"+msg);
        // msg.getDeviceList().get(1).getDeviceName();
    }
    @ApiOperation(value = "**************************设备指令响应通知************************")
    @PostMapping(value = "/test")
    public String test(@RequestBody String msg)
            throws Exception {
        System.err.println(msg);
        return "响应测试";
    }

    @RequestMapping(value = "/testNotify")
    public void testNotify(@RequestBody String msg)
            throws Exception {
        System.err.println(msg);
    }
}
